package com.beikbank.cloud.controller;


import com.beikbank.cloud.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by wangzhanjin on 2017/10/10.
 */
@RestController
public class MovieController {

     @Autowired
     private RestTemplate restTemplate;
     @Autowired
     private LoadBalancerClient loadBalancerClient;
     @GetMapping("/movie/{id}")
     public User findById(@PathVariable Long id){
          //http://localhost:7900/simple/
          //VIP virtual IP
          //HAProxy Heartbeat
         return this.restTemplate.getForObject("http://microservice-provider-user/simple/"+id, User.class);
     }

     @GetMapping("/test")
     public String test(){
          ServiceInstance serviceInstance = loadBalancerClient.choose("microservice-provider-user");
          System.out.println("111:"+serviceInstance.getHost()+":"+serviceInstance.getPort()+":"+serviceInstance.getServiceId());
          ServiceInstance serviceInstance2 = loadBalancerClient.choose("microservice-provider-user2");
          System.out.println("222:"+serviceInstance2.getHost()+":"+serviceInstance.getPort()+":"+serviceInstance.getServiceId());
          return "1";
     }
}
